Skip to content

feat(backend): add OpenAPI spec for the fulfillments API - #21

Merged
colinjlacy merged 9 commits into
runtimeconditions:mainfrom
mrunalC27:feat/openapi-spec
Nov 29, 2026
Merged

colinjlacy merged 9 commits into
runtimeconditions:mainfrom
mrunalC27:feat/openapi-spec

Conversation

@mrunalC27

@mrunalC27 mrunalC27 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #19

What and why

Generates an OpenAPI spec for the backend's /fulfillments endpoints from the routes registered on the Express router.

  • Request/response shapes are defined once in types.ts as zod schemas. The TS types (z.infer), request validation, and the spec all come from them, so there's no separate file of types to maintain.
  • Routes are registered on the router as usual. Each handler is wrapped with validated(), which validates the request and attaches the route's docs to the handler.
  • generateOpenApiDocument() walks router.stack, reads each route's path, method, and docs, and builds the spec with @asteasolutions/zod-to-openapi. A route without validated() fails generation.
  • npm run openapi:generate writes backend/dist/openapi.yaml (not committed). The release job generates it with the release version and attaches it to the GitHub release.
  • The API behaves the same: invalid requests still return 400 { error }, now with a message listing the failing fields.

Note: router.stack is an Express internal, though it's stable and widely used (e.g. by express-list-endpoints).

How it was tested

  • npm test passes in the root and in backend/
  • The README is updated if behavior or the API changed
  • A breaking change is marked with ! in the title and described above

Added tests that every registered route appears in the spec, and that an undocumented route fails generation.

Comment thread .github/workflows/release.yml Outdated
git tag "v$version"
git push origin "v$version"
gh release create "v$version" --generate-notes --verify-tag
gh release create "v$version" backend/src/schema/openapi.yaml --generate-notes --verify-tag No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for this PR, but please note that the entire release condition should be removed from this workflow as part of #22. We can address it in a PR that fixes that PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks! I'll handle that in #22.

Comment thread backend/src/schema/openapi.yaml Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is quite right. There should be a way to leverage the routes added to the router dynamically and generate the OpenAPI spec from that.

Comment thread backend/src/schema/openapi.generated.ts Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing it this way, the OpenAPI spec should be able to derive its shape from the TS types applied to the newly-added routes. That would make this file no longer necessary.

@colinjlacy colinjlacy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the spirit of keeping the code and API shape aligned is definitely the right approach. But I think the flow of data is going in the wrong direction. The existing TS code describes the endpoint behavior to the Express router in Backstage, and there are ways for Express to then generate the OpenAPI spec from the routes that have been registered against it. Can we take that approach instead?

@mrunalC27

Copy link
Copy Markdown
Contributor Author

I looked into it: walking the router's registered routes (router.stack) gives us each path and method, but Express doesn't know the request/response shapes, because the TS types are erased at compile time (req.body as FulfillmentRecord is gone at runtime). So the route needs to carry its shape in some form. Two options:

A) zod (my preference): routes stay registered on the Express router as usual, and each handler is wrapped with a small validated() helper that attaches a zod schema (zod is already a dependency of @backstage/backend-plugin-api). The generator walks router.stack, reads each route's path, method, and schema, and builds the spec with @asteasolutions/zod-to-openapi. Validation and the spec come from the same schema, the TS types are derived from it, and an undocumented route fails generation. I have this working locally with tests passing.

B) TS types first: keep the interfaces in types.ts and generate JSON schemas from them at build time (ts-json-schema-generator), with each route declaring which type it uses. This is closer to deriving from the TS types directly, but request validation would need to be wired up separately.

Either way, openapi.yaml, openapi.generated.ts, and the drift check go away, and the spec is built during release and attached as before. One caveat for both: router.stack is an Express internal, though it's stable and widely used (e.g. by express-list-endpoints).

Which do you prefer? Also, the backend-plugin-api bump was only needed for backend-openapi-utils. Keep it or revert it?

@colinjlacy

Copy link
Copy Markdown
Contributor

Let’s give the Zod implementation a try. I want to maintenance to be as minimal as possible so the more we can derive directly from the existing types, the better. Having to maintain a separate file with analogous types is going to be a challenge

@colinjlacy

Copy link
Copy Markdown
Contributor

Let’s revert the dependency upgrade for now

@colinjlacy colinjlacy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done! 🥳

@colinjlacy
colinjlacy merged commit 3087766 into runtimeconditions:main Nov 29, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI spec documentation for plugin REST APIs.

2 participants